package com.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.utils.StringUtils;
import com.project.domain.bo.CooperateUnitBo;
import com.project.domain.entity.BizProjectCooperationUnit;
import com.project.domain.entity.zxProject.BizZxProject;
import com.project.mapper.ProjectCooperationUnitMapper;
import com.project.mapper.zxProject.ZxProjectMapper;
import com.project.service.IProjectCooperationUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 项目合作单位Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-08
 */
@Service
public class ProjectCooperationUnitServiceImpl implements IProjectCooperationUnitService
{
    @Autowired
    private ProjectCooperationUnitMapper bizProjectCooperationUnitMapper;

    @Autowired
    private ZxProjectMapper zxProjectMapper;

    @Autowired
    private ProjectInitServiceImpl projectInitService;

    /**
     * 查询项目合作单位
     * 
     * @param ID 项目合作单位主键
     * @return 项目合作单位
     */
    @Override
    public BizProjectCooperationUnit selectBizProjectCooperationUnitByID(String ID)
    {
        return bizProjectCooperationUnitMapper.selectBizProjectCooperationUnitByID(ID);
    }

    /**
     * 查询项目合作单位列表
     * 
     * @param bizProjectCooperationUnit 项目合作单位
     * @return 项目合作单位
     */
    @Override
    public List<BizProjectCooperationUnit> selectBizProjectCooperationUnitList(BizProjectCooperationUnit bizProjectCooperationUnit)
    {
        return bizProjectCooperationUnitMapper.selectBizProjectCooperationUnitList(bizProjectCooperationUnit);
    }

    /**
     * 查询项目合作单位列表
     *
     * @param proId 项目合作单位
     * @return 项目合作单位
     */
    public List<BizProjectCooperationUnit> selectBizProjectCooperationUnitListNew(String proId)
    {
        List<BizProjectCooperationUnit> res = new ArrayList<>();
        try{
            res = bizProjectCooperationUnitMapper.selectBizProjectCooperationUnitListNew(proId);
        }catch (Exception exception){
            String ex= exception.getMessage();
        }
        return res;
    }

    /**
     * 新增项目合作单位
     * 
     * @param bizProjectCooperationUnit 项目合作单位
     * @return 结果
     */
    @Override
    public int insertBizProjectCooperationUnit(BizProjectCooperationUnit bizProjectCooperationUnit)
    {
        return bizProjectCooperationUnitMapper.insertBizProjectCooperationUnit(bizProjectCooperationUnit);
    }

    /**
     * 修改项目合作单位
     * 
     * @param bizProjectCooperationUnit 项目合作单位
     * @return 结果
     */
    @Override
    public int updateBizProjectCooperationUnit(BizProjectCooperationUnit bizProjectCooperationUnit)
    {
        return bizProjectCooperationUnitMapper.updateBizProjectCooperationUnit(bizProjectCooperationUnit);
    }

    /**
     * 批量删除项目合作单位
     * 
     * @param IDs 需要删除的项目合作单位主键
     * @return 结果
     */
    @Override
    public int deleteBizProjectCooperationUnitByIDs(String[] IDs)
    {
        return bizProjectCooperationUnitMapper.deleteBizProjectCooperationUnitByIDs(IDs);
    }

    /**
     * 删除项目合作单位信息
     * 
     * @param ID 项目合作单位主键
     * @return 结果
     */
    @Override
    public int deleteBizProjectCooperationUnitByID(String ID)
    {
        return bizProjectCooperationUnitMapper.deleteBizProjectCooperationUnitByID(ID);
    }

    //region 项目立项和变更中用的一些方法
    /**
     * 保存纵向项目合作单位信息
     * @param cooperateUnitBo
     * @return
     */
    public String saveCooperUnitList(CooperateUnitBo cooperateUnitBo){
        String res = "保存成功！";
        try{
            if(cooperateUnitBo != null && StringUtils.isNotBlank(cooperateUnitBo.getProId())){
                String proId = cooperateUnitBo.getProId();

                BizZxProject bizZxProject = new BizZxProject();
                bizZxProject.setFormId(cooperateUnitBo.getFormId());//组织形式
                bizZxProject.setLeadUnitName(cooperateUnitBo.getLeadUnitName());//牵头单位
                bizZxProject.setCooperateId(cooperateUnitBo.getCooperateId());//合作形式
                bizZxProject.setId(proId);
                zxProjectMapper.updateById(bizZxProject);

                //先删除
                deleteByProId(proId);

                List<BizProjectCooperationUnit> unitList = cooperateUnitBo.getCooperationUnits();
                if(unitList != null && unitList.size() > 0){
                    if(StringUtils.isNotBlank(proId)){
                        for(int m = 0;m<unitList.size();m++){
                            BizProjectCooperationUnit unitInfo = unitList.get(m);
                            unitInfo.setOrderId((long)m+1);
                            unitInfo.setContractFee(unitInfo.getContractFee().multiply(new BigDecimal("10000")));
                            bizProjectCooperationUnitMapper.insert(unitInfo);
                        }
                    }
                }
            }
        }catch (Exception exception){
            res = "保存合作单位信息失败！" + exception.getMessage();
        }

        return res;
    }

    /**
     * 删除合作单位信息(根据项目号删除)
     * @param proId 项目号
     * @return
     */
    public String deleteByProId(String proId){
        String res= "删除成功！";
        try{
            QueryWrapper<BizProjectCooperationUnit> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PROJECT_ID",proId);
            bizProjectCooperationUnitMapper.delete(queryWrapper);
        }catch (Exception exception){
            res = "删除合作单位信息失败！" + exception.getMessage();
        }

        return res;
    }
    //endregion
}
